package com.future.mashibing.class06;

import com.future.util.TreeNode;

/**
 * 所谓先序后续中序，是相对于当前节点（头结点）确定的
 */
public class TraversBinaryTree {

    public static void main(String[] args) {
        TreeNode head = new TreeNode(1);
        head.left = new TreeNode(2);
        head.right = new TreeNode(3);
        head.left.left = new TreeNode(4);
        head.left.right = new TreeNode(5);
        head.right.left = new TreeNode(6);
        head.right.right = new TreeNode(7);
        preOrder(head);
        System.out.println();
        middleOrder(head);
        System.out.println();
        postOrder(head);
    }

    /**
     * 先序遍历：中左右
     */
    public static void preOrder(TreeNode head) {
        if (head == null) {
            return;
        }
        System.out.print(head.val);
        preOrder(head.left);
        preOrder(head.right);
    }

    /**
     * 中序遍历：左中右
     */
    public static void middleOrder(TreeNode head) {
        if (head == null) {
            return;
        }
        middleOrder(head.left);
        System.out.print(head.val);
        middleOrder(head.right);
    }

    /**
     * 后序遍历：左右后
     */
    public static void postOrder(TreeNode head) {
        if (head == null) {
            return;
        }
        postOrder(head.left);
        postOrder(head.right);
        System.out.print(head.val);
    }
}
